package com.wang.commons.utils;

import java.lang.reflect.Field;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CommonsUtil {
    //判断对象是否有空属性
    public static <T> Boolean hasAllFields(T t) throws IllegalAccessException {
        if(t==null) return false;
        Class<?> clazz = t.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (field.get(t)==null) return false;
        }
        return true;
    }
    //随机数字验证码 0-9
    public static String getCode(int length){
        Random random = new Random();
        StringBuilder code = new StringBuilder();
        for(int i =0;i<length;i++){
        code.append(random.nextInt(10));
        }
        return code.toString();
    }
    public static void main(String[] args) throws IllegalAccessException {
    }

    //obj转list
        public static <T> List<T> castList(Object obj, Class<T> clazz)
    {
        List<T> result = new ArrayList<T>();
        if(obj instanceof List<?>)
        {
            for (Object o : (List<?>) obj)
            {
                result.add(clazz.cast(o));
            }
            return result;
        }
        return null;
    }
    //日期转换
    public static String parseDateLong(Long time,String format){
        SimpleDateFormat sd = new SimpleDateFormat(format);
        return sd.format(new Date(time));
    }
    public static Long parseDateString(String time,String format) throws ParseException {
        SimpleDateFormat sd=new SimpleDateFormat(format);
        return sd.parse(time).getTime();
    }
}
